//
//  AEHomePageSubViewController.m
//  AESOLAR_IOS
//
//  Created by Bin Tang on 2024/10/18.
//  Copyright © 2024 Mr.Wang. All rights reserved.
//

#import "AEHomePageSubViewController.h"
#import "AEHomePageSubViewModel.h"
#import "AEHomePageSubTopView.h"
#import "AEHomePageSubViewCell.h"
#import "AECommunityDetailViewController.h" // 详情
#import "AEShareInfoView.h" // 分享

@interface AEHomePageSubViewController ()
@property (nonatomic, strong) AEHomePageSubViewModel *viewModel;

@property (nonatomic, strong) AEHomePageSubTopView *headerView;

@property (nonatomic, strong) AEShareInfoView *shareView;

@end

@implementation AEHomePageSubViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [AEHomePageSubViewModel new];
    _viewModel.newsGroup = self.newsGroup;
    _viewModel.newsGroupId = self.newsGroupId;
    [_viewModel setupData];
}

- (void)initWithSubView {
    [super initWithSubView];
    self.view.backgroundColor = kWhiteColor;
    self.baseTableView.backgroundColor = kWhiteColor;
    self.baseTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.baseTableView registerClass:[AEHomePageSubViewCell class] forCellReuseIdentifier:[AEHomePageSubViewCell className]];
//    self.baseTableView.contentOffset = CGPointMake(0, 300);
    self.baseTableView.contentInset = UIEdgeInsetsMake(kAEHomePageSubTopViewHeight, 0, 0, 0);
    self.baseTableView.contentOffset = CGPointMake(0, -kAEHomePageSubTopViewHeight);
    
    [self doRefreshData];
    
    [self initWithShareView];
}

- (void)initWithHeaderView {
    [super initWithHeaderView];
    
    if (!self.headerView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kAEHomePageSubTopViewHeight);
        _headerView = [[AEHomePageSubTopView alloc] initWithFrame:frame];
        [self.view addSubview:_headerView];
        @weakify(self);
        _headerView.touchSearchBlock = ^{
            @strongify(self);
            [self doRefreshData];
        };
    }
    
    [self.headerView updateWithViewData:self.viewModel];
}

/// 分享
- (void)initWithShareView {
    if (!_shareView) {
        float height = 226 + IPHONEX_BOTTOM_SPACE;
        CGRect frame = CGRectMake(0, kScreenH - height , kScreenW, height);
        _shareView = [[AEShareInfoView alloc] initWithFrame:frame];
    }
}


- (void)reloadData {
    [self doRefreshData];
}

- (void)doRefreshData {
    [super doRefreshData];
    
    self.viewModel.reqModel.pageNum = 1;
    
    [self fromServerRequestData];
}

- (void)doMoreRefreshData {
    [super doMoreRefreshData];
    
    [self fromServerRequestData];
}

- (void)fromServerRequestData {
    @weakify(self);
    [self.viewModel requestWithNewsInfos:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            [self.baseTableView reloadData];
        }
        [self endAllRefreshing];
    }];
}

/// 详情
/// - Parameter model: model description
- (void)actionWithCell:(AENewsInfoModel *)model {
    AECommunityDetailViewController *detailVC = [[AECommunityDetailViewController alloc] init];
    detailVC.newsInfoModel = model;
    [TYZJumpVCManager pushBaseVC:self pushVC:detailVC];
}

/// 点赞
/// - Parameter model: model description
- (void)actionWithPraise:(AENewsInfoModel *)model indexPath:(NSIndexPath *)indexPath {
    @weakify(self);
    [SVProgressHUD showWithStatus:@"操作中..."];
    NSInteger newsId = model.id;
    NSInteger like = (model.like == YES ? 0 : 1);
    NSString *err = (like == 1 ? @"点赞失败，请稍后再试！" : @"取消点赞失败，请稍后再试！");
    NSString *suc = (like == 1 ? @"点赞成功" : @"取消点赞成功");
    [self.viewModel requestWithNewsLikeNewsId:newsId like:like completion:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
        if (respond.code == EDS_NETWORK_SUCCESS) {
            model.like = !model.like;
            if (model.like) {
                model.newsLikes += 1;
            } else {
                model.newsLikes -= 1;
            }
            [self.baseTableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [SVProgressHUD showSuccessWithStatus:suc];
        } else {
            [SVProgressHUD showInfoWithStatus:err];
        }
    }];
}

/// 分享
/// - Parameter model: model description
- (void)actionWithShare:(AENewsInfoModel *)model {
//    [SVProgressHUD showInfoWithStatus:@"分享"];
    @weakify(self);
    [[AEAppDelegateTool sharedInstance] requestWithSysConfig:^(EDSRespondDataEntity * _Nonnull respond) {
        @strongify(self);
//        [[AEAppDelegateTool sharedInstance] showShareVC:self];
        [self showShareVC:model];
    }];
}

/// 显示分享弹窗
- (void)showShareVC:(AENewsInfoModel *)model {
    @weakify(self);
    NSString *shareUrl = [NSString stringWithFormat:@"%@photovoltaic-share/news/detail.html?id=%zd", kBaseUrl, model.id];
    [_shareView showWithTitle:@"立即分享给好友" data:nil completion:^(id  _Nonnull data) {
        @strongify(self);
        NSInteger tag = [data integerValue];
        UIImage *image = [UIImage imageNamed:@"logo"];
        if (tag == 100) { // 微信
            [EDSUtility shareWithTitle:model.newsTitle desc:@"" shareUrl:shareUrl shareType:1 image:image];
        } else if (tag == 101) { // 朋友圈
            [EDSUtility shareWithTitle:model.newsTitle desc:@"" shareUrl:shareUrl shareType:2 image:image];
        }
        [self.shareView hide];
    } failed:^{
        @strongify(self);
        [self.shareView hide];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = self.viewModel.dataList.count;
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kAEHomePageSubViewCellHeight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AENewsInfoModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    AEHomePageSubViewCell *cell = [AEHomePageSubViewCell cellForTableView:tableView indexPath:indexPath];
    [cell updateCellData:model];
    @weakify(self);
    cell.baseTableViewCellBlock = ^(id data) {
        @strongify(self);
        [self actionWithCell:model];
    };
    // 点赞/取消
    cell.touchPraiseBlock = ^{
        @strongify(self);
        [self actionWithPraise:model indexPath:indexPath];
    };
    
    // 分享
    cell.touchShareBlock = ^{
        @strongify(self);
        [self actionWithShare:model];
    };
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AENewsInfoModel *model = [self.viewModel.dataList objectOrNilAtIndex:indexPath.row];
    [self actionWithCell:model];
}


@end
